/**
 * 自定义函数
 * */

var userLogin; //用户信息
var routes = {
    index: '/index.html',                 //入口路由
    scence: '/views/scence.html',         //场景路由
    tmp1Index: '/views/tmp1/index.html', //模板详情路由
    tmp1Add: '/views/tmp1/add.html'      //模板新增路由
}; //前端路由地址
var apis = {
    userInfo: {                           //用户信息
        method: 'post',
        url: '/h5/user/info'
    },
    userInfoUpdate: {                     //修改用户信息
        method: 'post',
        url: '/h5/user/info/modify'
    },
    //sendYzm: {                            //发送验证码
    //    method: 'post',
    //    url: '/h5/user/sendyzm'
    //},
    //userPhone: {                           //保存手机号
    //    method: 'post',
    //    url: '/h5/user/phone'
    //},
    qrEmpty: {                            //判断码空不空
        method: 'get',
        url: '/h5/qr/empty'
    },
    scenceVideoList: {                    //视频列表
        method: 'get',
        url: '/h5/scence/video/list'
    },
    scenceVideoPlay: {                    //视频播放
        method: 'post',
        url: '/h5/scence/video/play'
    },
    scenceVideoDelay: {                    //视频时长
        method: 'post',
        url: '/h5/scence/video/delay'
    },
    tmp1Add: {                            //场景1新增
        method: 'post',
        url: '/h5/tmp1/add'
    },
    tmp1One: {                            //场景1详情
        method: 'get',
        url: '/h5/tmp1/one'
    },
    tmp1CommentIndex: {                 //场景1评论列表
        method: 'get',
        url: '/h5/tmp1/comment/index'
    },
    tmp1CommentAdd: {                 //场景1评论新增
        method: 'post',
        url: '/h5/tmp1/comment/add'
    },
    tmp1HistoryIndex: {                 //场景1历史列表
        method: 'get',
        url: '/h5/tmp1/history/index'
    },
    tmp1FollowIndex: {                 //场景1关注列表
        method: 'get',
        url: '/h5/tmp1/follow/index'
    },
    tmp1FollowModify: {                 //场景1关注设置
        method: 'post',
        url: '/h5/tmp1/follow/modify'
    },
    shop1Index: {                 //店面1详情【场景2】
        method: 'get',
        url: '/h5/shop1/index'
    },
    shop1One: {                 //店面1详情【场景2】
        method: 'get',
        url: '/h5/shop1/one'
    },
    shop1Add: {                 //店面1新增
        method: 'post',
        url: '/h5/shop1/add'
    },
    shop1CateList: {                 //店面1分类列表【场景2】
        method: 'get',
        url: '/h5/shop1/cate/list'
    },
    shop1GoodsList: {                 //店面1商品列表【场景2】
        method: 'get',
        url: '/h5/shop1/goods/list'
    },
    shop1CommentList: {                 //店面1评论列表【场景2】
        method: 'get',
        url: '/h5/shop1/comment/list'
    },
    shop1CommentAdd: {                 //店面1评论新增【场景2】
        method: 'post',
        url: '/h5/shop1/comment/add'
    },
    shop1FollowList: {                 //店面1关注列表【场景2】
        method: 'get',
        url: '/h5/shop1/follow/list'
    },
    shop1FollowModify: {                 //店面1关注设置【场景2】
        method: 'post',
        url: '/h5/shop1/follow/modify'
    },
    shop1ExistGoodsFollow: {             //店面1关注判断【场景2】
        method: 'get',
        url: '/h5/shop1/goods/follow/exist'
    },
    shop1GoodsFollowAdd: {              //店面1关注商品新增【场景2】
        method: 'post',
        url: '/h5/shop1/goods/follow/add'
    },
    shop1CartList: {                    //店面1购物车列表【场景2】
        method: 'get',
        url: '/h5/shop1/cart/list'
    },
    shop1CartAdd: {                    //店面1购物车新增【场景2】
        method: 'post',
        url: '/h5/shop1/cart/add'
    },
    shop1CartDel: {                    //店面1购物车删除【场景2】
        method: 'post',
        url: '/h5/shop1/cart/del'
    },
    shop1OrderOne: {                    //店面1订单列表【场景2】
        method: 'get',
        url: '/h5/shop1/order/one'
    },
    shop1OrderAdd: {                    //店面1订单添加【场景2】
        method: 'post',
        url: '/h5/shop1/order/add'
    },
    shop1OrderDel: {                    //店面1订单删除【场景2】
        method: 'post',
        url: '/h5/shop1/order/del'
    }
}; //后端接口地址


// 获取indentify
function getUid(){
    var code = getParamByUrl('code');
    var userInfo = getUserInfo();
    return code + '<>' + userInfo.userId;
}


//APP弹窗
function appPop(){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var userInfo = getUserInfo();
    if (!userInfo || !userInfo.userId) {
        msgPop({msg: '未登录'}); return
    }
    var qrlink = domain + '?userId=' + userInfo.userId;
    var h = '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;" onclick="$(\'#appPop\').html(\'\');"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.6) + 'px; text-align: center; background: #fff; border-radius: 5px; position: fixed; top: 20%; left: ' + ((cw * 0.4 - 40) / 2) + 'px; z-index: 10000;">';
    h += '<a href="' + domain + '/downloads/freecode.apk' + '" style="text-decoration: none;">';
    h += '<div style="padding: 10px; color: #0088cc;">下载APP去编辑</div>';
    h += '</a>';
    h += '<div style="font-size: 12px; color: #888;">通过APP扫以下码同步</div>';
    h += '<div id="qrBox" onclick="alert(\'' + qrlink + '\');" style="margin: 10px auto; width: 180px; height: 180px; background: #eee;"></div>';
    h += '<div onclick="$(\'#appPop\').html(\'\');" style="margin: 10px auto; color: red;">关闭</div>';
    h += '</div>';
    h += '</div>';
    $('#appPop').html(h);
    $('#qrBox').qrcode({
        render: 'canvas', text: qrlink,
        width: 180, height: 180,
        background: "#fff", //背景颜色
        foreground: "#000", //前景颜色
        //src: '/imgs/bai.png'
    });
}


////AJAX请求
//function toAjax(e){
//    $.ajax({
//        type: e.api.method,
//        url: e.api.url,
//        data: e.data,
//        success: function (res) {
//            console.log(res);
//            if (res.code != 0) {
//                msgPop({msg: res.msg}); return
//            }
//            if (e.callback != '') {
//                e.callback();
//            }
//        },
//        error: function () {
//            console.log('网络错误');
//        }
//    });
//}


//显示前几个字符
function strPrefix(str, limit){
    if (str.length > limit) {
        return str.substring(0, limit) + '...';
    } else {
        return str;
    }
}


//显示后几个字符
function strSuffix(str, limit){
    //console.log(str.length, limit);
    if (str.length > limit) {
        return '...' + str.slice(- limit);
    } else {
        return str;
    }
}


//自定义弹窗
function msgPop(e){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var msg = e.msg;
    var h = '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch - 35) + 'px; background: #000; opacity: 0.2; position: fixed; top: 35px; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.5) + 'px; text-align: center; background: #000; color: #FFF; border-radius: 5px; position: fixed; bottom: 40%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">'+msg+'</div>';
    h += '</div>';
    h += '<script>setTimeout(function(){$(\'#msgPop\').html(\'\');},2000)</script>';
    $('#msgPop').html(h);
}


//抓图
function uploadImage() {
    var img = $("#viewimg"), dataURL, fileObj = $('#uploadFile')[0],
        windowURL = window.URL || window.webkitURL, reader = new FileReader();
    dataURL = windowURL.createObjectURL(fileObj.files[0]); //要显示图片的路径
    img.attr("src", dataURL); //显示图片
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (e){
        //console.log(e.target.result);
        //$('#img')[0].value = e.target.result; //这一步是关键
        showImg(e.target.result);
    }
}


//请求接口获取用户标识
function userInfo(){
    if (code == '') {
        msgPop({msg: '没有码值'}); return
    }
    var userInfo = getUserInfo();
    //delCache({key: userLogin}); return
    if (!userInfo) {
        $.ajax({
            type: 'post',
            url: domain+'/h5/user/info',
            data: {ucode: userInfo.ucode},
            success: function (res) {
                console.log(res);
                if (res.code != 0) {
                    console.log(res);
                }
                var userInfo = {
                    userId: res.data.userId
                };
                setUserInfo({val: userInfo});
            },
            error: function () {
                console.log('网络错误');
            }
        });
    }
}


//获取登录信息
function getUserInfo(){
    return getCache(userLogin);
}


//设置登录信息
function setUserInfo(e){
    return setCache({key: userLogin, val: e.val});
}


//获取地址栏中参数
function getParamByUrl(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r==null) return '';
    return  unescape(r[2]);
}


//设置缓存
function setCache(e){
    var key = e.key, val = JSON.stringify(e.val);
    window.localStorage.setItem(key, val);
}


//获取缓存
function getCache(key){
    var cache = window.localStorage.getItem(key);
    if (!cache) { return ''; }
    return JSON.parse(cache);
}


//删除指定缓存
function delCache(e){
    var key = e.key;
    window.localStorage.removeItem(key);
}


//清理缓存
function clearCache(){
    window.localStorage.clear();
}